/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.web.client.servlet;

import com.imenu.ejb.ImenuMenuFacade;
import com.imenu.entity.ImenuCourse;
import com.imenu.entity.ImenuMenu;
import com.imenu.entity.ImenuTag;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yunzi
 */
@WebServlet(name = "ClientCourseServlet", urlPatterns = {"/clientCourse"})
public class ClientCourseServlet extends BaseServlet<ImenuMenu> {

    @EJB ImenuMenuFacade menuFacade;
    /**如果command不能match到任何方法,默认显示的页面*/
    void courseList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("MobileServlet.courseList() request.getParameter(menuName)=" + request.getParameter("menuName"));
        System.out.println("MobileServlet.courseList() request.getParameter(menuId)=" + request.getParameter("menuId"));
        Integer menuId = Integer.valueOf(request.getParameter("menuId"));
        ImenuMenu menu = menuFacade.find(menuId);
        TreeMap<ImenuTag, List<ImenuCourse>> courseByTag = new TreeMap<ImenuTag, List<ImenuCourse>>();
        for (ImenuCourse course : menu.getImenuCourseCollection()) {
            ImenuTag tag = course.getTag();
            if (courseByTag.get(tag) == null) {
                courseByTag.put(tag, new ArrayList<ImenuCourse>());
            }
            courseByTag.get(tag).add(course);
        }
        List<ImenuTag> tags = new ArrayList<ImenuTag>();
        List<List<ImenuCourse>> coursesLists = new ArrayList<List<ImenuCourse>>();
        for (ImenuTag tag : courseByTag.keySet()) {
            tags.add(tag);
            coursesLists.add(courseByTag.get(tag));
        }
        String tagIdParam = request.getParameter("tagId");
        ImenuTag preTag = null;
        ImenuTag curTag = null;
        ImenuTag nexTag = null;

        if (tagIdParam == null) {
            if (tags.size() > 0) {
                curTag = tags.get(0);
            }
            if (tags.size() > 1) {
                nexTag = tags.get(1);
            }
        } else {
            try {
                Integer tagId = Integer.valueOf(tagIdParam);
                for (int i = 0; i < tags.size(); i++) {
                    if (tags.get(i).getId().equals(tagId)) {
                        //System.out.println("bingo i="+i+" tags.size() = "+tags.size());
                        curTag = tags.get(i);
                        if (i > 0) {
                            preTag = tags.get(i - 1);
                        }
                        if ((tags.size() - i) > 1) {
                            nexTag = tags.get(i + 1);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        List<ImenuCourse> courses = courseByTag.get(curTag);
        //System.out.println("tagId="+tagIdParam);
        //System.out.println("preTag="+preTag);
        //System.out.println("curTag="+curTag);
        //System.out.println("nexTag="+nexTag);

        request.setAttribute("menuId", menuId);
        request.setAttribute("preTag", preTag);
        request.setAttribute("curTag", curTag);
        request.setAttribute("nexTag", nexTag);
        request.setAttribute("courses", courses);
        forward(request, response, "/course/" + request.getParameter("command") + "1.jsp");
    }
}
